package com.tianliangedu.utils;

import java.util.List;

/**
 * String常用操作类
 * 
 * @author zel
 * 
 */
public class StringOperatorUtil {
	private RegexParserUtil regexPaserUtil = null;

	public StringOperatorUtil() {
		regexPaserUtil = new RegexParserUtil();
	}

	public static boolean isBlank(String str) {
		if (str == null || str.trim().length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isBlankCollection(List list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	public static void getAllWordCompFromStartToEnd(String query,
			List<String> wordList) {
		if (query.length() < 2) {
			return;
		}
		int i;
		int j;
		for (i = 0; i < query.length() - 1; i++) {// 该遍历保证遍历到query的倒数第二个字符开始
			for (j = 2; i + j <= query.length(); j++) {
				wordList.add(query.substring(i, i + j));
			}
		}
	}

	public static boolean isNotBlank(String str) {
		if (str == null || str.trim().length() == 0) {
			return false;
		}
		return true;
	}

	public static boolean isNotBlankCollection(List list) {
		if (list == null || list.isEmpty() || list.size()==0) {
			return false;
		}
		return true;
	}

	public boolean isAllEnglish(String str) {
		if (StringOperatorUtil.isBlank(str)) {
			return false;
		}
		return regexPaserUtil.isOnlyAlphaOrDigital(str);
	}

	public boolean isAlphaOrDigital4EnglishWordBeside(String bigStr,
			String subStr) {
		int pos = bigStr.indexOf(subStr);
		if (pos < 0) {
			return false;
		}
		int pos_front = pos - 1;
		if (pos_front >= 0) {
			if (regexPaserUtil.isOnlyAlphaOrDigital(bigStr.substring(pos_front,
					pos_front + 1))) {
				return true;
			}
		}
		int aid_pos = pos + subStr.length();
		if (aid_pos < bigStr.length()) {
			if (regexPaserUtil.isOnlyAlphaOrDigital(bigStr.substring(aid_pos,
					aid_pos + 1))) {
				return true;
			}
		}
		return false;
	}

	public boolean isBlank4EnglishWordBeside(String bigStr, String subStr) {
		int pos = bigStr.indexOf(subStr);
		if (pos < 0) {
			return false;
		}
		int pos_front = pos - 1;
		if (pos_front >= 0) {
			if (StringOperatorUtil.isNotBlank(bigStr.substring(pos_front,
					pos_front + 1))) {
				return false;
			}
		}
		int aid_pos = pos + subStr.length();
		if (aid_pos < bigStr.length()) {
			if (StringOperatorUtil.isNotBlank(bigStr.substring(aid_pos,
					aid_pos + 1))) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		// String query = "12";
		// List<String> wordList = new ArrayList<String>();
		// getAllWordCompFromStartToEnd(query, wordList);
		// System.out.println(wordList);
		StringOperatorUtil stringOperatorUtil = new StringOperatorUtil();

		String big = "EMBA研修班成23";
		String little = "EMBA研修班";

		System.out.println(stringOperatorUtil.isAllEnglish(little));
		System.out.println(stringOperatorUtil
				.isAlphaOrDigital4EnglishWordBeside(big, little));

		// String str="emba学费对外经济贸易大学";
		// List<String> list=new ArrayList<String>();
		//
		// getAllWordCompFromStartToEnd(str,list);
		//
		// for(String line:list){
		// System.out.println(line);
		// }

		System.out.println("done!");

	}
}
